package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"time"
)

type ActivityInfoService struct {
}

// CreateActivityInfo 创建ActivityInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (activityInfoService *ActivityInfoService) CreateActivityInfo(activityInfo autocode.ActivityInfo) (err error) {
	err = global.GVA_DB.Create(&activityInfo).Error
	return err
}

// DeleteActivityInfo 删除ActivityInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (activityInfoService *ActivityInfoService) DeleteActivityInfo(activityInfo autocode.ActivityInfo) (err error) {
	err = global.GVA_DB.Delete(&activityInfo).Error
	return err
}

// DeleteActivityInfoByIds 批量删除ActivityInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (activityInfoService *ActivityInfoService) DeleteActivityInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.ActivityInfo{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateActivityInfo 更新ActivityInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (activityInfoService *ActivityInfoService) UpdateActivityInfo(activityInfo autocode.ActivityInfo) (err error) {
	err = global.GVA_DB.Save(&activityInfo).Error
	return err
}

// GetActivityInfo 根据id获取ActivityInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (activityInfoService *ActivityInfoService) GetActivityInfo(id uint) (err error, activityInfo autocode.ActivityInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&activityInfo).Error
	return
}

// GetActivityInfoInfoList 分页获取ActivityInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (activityInfoService *ActivityInfoService) GetActivityInfoInfoList(info autoCodeReq.ActivityInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.ActivityInfo{}).Order("create_time desc")
	var activityInfos []autocode.ActivityInfo
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.EndData != nil && info.StartData != nil {
		db.Where("activity_info.create_time >= ? AND activity_info.create_time <= ?", time.Unix(info.StartData.Unix(), 0), time.Unix(info.EndData.Unix()+86400-1, 0))
	}
	if info.UserId != nil && *info.UserId != 0 {
		db = db.Where("user_id = ?", info.UserId)
	}
	if info.ActivityType != nil && *info.ActivityType != 0 {
		db = db.Where("activity_type = ?", info.ActivityType)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	selects := "user_info.user_name, user_info.open_id, activity_info.created_at, activity_info.create_time, activity_info.id, activity_info.marks, activity_info.time, activity_info.activity_type, activity_info.user_id"
	err = db.Limit(limit).Offset(offset).Joins("left join user_info on user_info.id = activity_info.user_id").Select(selects).Find(&activityInfos).Error
	return err, activityInfos, total
}
